<template>
  <el-container class="home">
    <el-header>
      <div class="logo_box">
        <img src="../assets/heima.png" alt />
        <span>vue 后台管理</span>
      </div>
      <el-button type="info" @click="out">退出登录</el-button>
    </el-header>
    <el-container class="aside">
      <!-- 侧边导航 -->
      <el-aside width="200px">
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          router
          :default-active="activeIndex"
        >
          <div class="shousuo" @click="shousuo">|||</div>
          <!-- 第一层  -->
          <el-submenu
            :index="item.id+''"
            v-for="item in asideList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 第二层 -->
            <el-menu-item
              :index="'/'+item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
              @click="checkItem('/'+item2.path)"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- Main -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: 'Home',
    data() {
      return {
        asideList: [],
        iconList: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-morentouxiang',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        isCollapse: false,
        activeIndex: '/users'
      }
    },
    created() {
      this.getAsideList()
      this.activeIndex = window.sessionStorage.getItem('path')
    },
    methods: {
      out() {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      async getAsideList() {
        const { data } = await this.$http.get('menus')
        if (data.meta.status !== 200) return this.$message.error(fdata.meta.msg)
        this.asideList = data.data
      },
      shousuo() {
        this.isCollapse = !this.isCollapse
      },
      checkItem(path) {
        this.activeIndex = path
        window.sessionStorage.setItem('path', path)
      }
    }
  }
</script>
<style lang="less" scoped>
  .home {
    height: 100%;
    .el-header {
      padding: 0 5px 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .logo_box {
        height: 60px;
        line-height: 60px;
        width: 500px;
        img {
          height: 100%;
          vertical-align: middle;
        }
        span {
          font-size: 26px;
          padding-left: 20px;
        }
      }
    }
    .aside {
      .el-menu {
        border: none;
      }
      .shousuo {
        font-size: 25px;
        text-align: center;
      }
      .iconfont {
        padding: 6px;
      }
    }
  }
</style>
